//
//
//

package qqopenapi_v3

import (
  "encoding/json"
  "log"
  "net"
  "time"
)

type SNSStat struct {}

func NewSNSStat() *SNSStat {
  return &SNSStat{}
}

func (this *SNSStat) GetTime() int64 {
  // or return time.Now().UnixNano()
  return time.Now().Unix()
}

func (this *SNSStat) StatReport(stat_url string, start_time int64, params map[string]interface{}) {
  endTime := this.GetTime()
  params["time"] = endTime - start_time
  params["timestatmp"] = endTime
  params["collect_point"] = "sdk-golang-v3"

  stat_data, err := json.Marshal(params) 
  if err != nil {
    log.Println(err) 
    return
  }

  var conn net.Conn
  conn, err = net.Dial("tcp", stat_url + ":80")
  if err != nil {
    log.Println(err)
    return
  }
  defer conn.Close()

  _, err = conn.Write(stat_data)
  if err != nil {
    log.Println(err)   
  }
}
